import Vue from 'vue'
import { MessageBox } from 'mint-ui';
import router from '@/store/router'
export default {
    //获取注册页面用户信息，如果注册过了，则提示已注册，跳到之前的页面
    getCommonUserInfo(){
        Vue.prototype.$newHttp.post('/userAccount/getUserInfo', {})
            .then(res => {
                // context.commit('setUserInfo', res.data);  
                if(res.data.accountType != 0){
                    MessageBox.alert('您已注册！').then(()=>{
                        router.push('/person');
                    })   
                }          
            })
            .catch(err => { console.log(err) });
    },
    //获取用户信息，如果未注册 则跳到注册页面
    getUserInfo(context){
        Vue.prototype.$newHttp.post('/userAccount/getUserInfo', {})
            .then(res => {
                context.commit('setUserInfo', res.data);  
                if(!res.data || res.data.accountType == 0){
                    router.push('/register');
                }          
            })
            .catch(err => { console.log(err) });
    },
    getCommonAddress(context){
        Vue.prototype.$newHttp.post('/userAddress/commonList', {})
            .then(res => {
                context.commit('setCommonAddress', res.data);
            })
            .catch(err => { console.log(err) });
    },
    getServiceAddress(context) {
        Vue.prototype.$newHttp.post('/userAddress/serviceList', {})
            .then(res => {
                context.commit('setServiceAddress', res.data);
            })
            .catch(err => { console.log(err) });
    },
    addCommonAddress(context, params) {
        Vue.prototype.$newHttp.post('/userAddress/insert', params)
            .then(res => {
                if (res.status === 0) {
                    MessageBox({
                        title: '提示',
                        message: res.message,
                    });
                }else if (res.status === 1) {
                    params.addressId = res.data.addressId;
                    context.commit('addCommonAddress', params);
                    MessageBox({
                        title: '提示',
                        message: '添加成功',
                    });
                }

            })
            .catch(err => { console.log(err) });
    },
    delCommonAddress(context, params) {
        Vue.prototype.$newHttp.post('/userAddress/delete', params)
            .then(res => {
                if (res.status === 0) {
                    MessageBox({
                        title: '提示',
                        message: res.message,
                    });
                } else if (res.status === 1) {
                    MessageBox.alert('删除成功').then(() => {
                        context.commit('delCommonAddress', params);
                    });
                }

            })
            .catch(err => { console.log(err) });
    },
    updateCommonAddress(context, params) {
        Vue.prototype.$newHttp.post('/userAddress/update', params)
            .then(res => {
                if (res.status === 0) {
                    MessageBox({
                        title: '提示',
                        message: res.message,
                    });
                } else if (res.status === 1) {
                    context.commit('updateCommonAddress', params);
                    MessageBox({
                        title: '提示',
                        message: '更改成功',
                    });
                }

            })
            .catch(err => { console.log(err) });
    },
    getOrderListData(context){
        Vue.prototype.$newHttp.post('/order/list',{})
            .then(res => {
                if (res.status === 0) {
                    MessageBox({
                        title: '提示',
                        message: res.message,
                    });
                } else if (res.status === 1) {
                    console.log(res);
                    context.commit('setOrderListData', res.data);
                }

            })
            .catch(err => { console.log(err) });
    },
    getRecharge(context){
        Vue.prototype.$newHttp.post('/userAddress/rechargeServiceList',{})
            .then(res => {
                if (res.status === 0) {
                    MessageBox({
                        title: '提示',
                        message: res.message,
                    });
                } else if (res.status === 1) {
                    context.commit('setRechargeData', res.data);
                }
                // console.log(res);
            })
            .catch(err => { console.log(err) });
    },
    userRegister(context,params){
        Vue.prototype.$newHttp.post('/userAccount/bindUserInfo',params)
            .then(res => {
                if (res.status === 0) {
                    MessageBox({
                        title: '提示',
                        message: res.message,
                    });
                } else if (res.status === 1) {
                    MessageBox.alert('注册成功').then(() => {
                        router.go(-1);
                    });    
                }
            })
            .catch(err => { console.log(err) });
    },
    getVerifyCode(context,params){
        Vue.prototype.$newHttp.post('/smsVerifyCode/send',params)
            .then(res => {
                if (res.status === 0) {
                    MessageBox({
                        title: '提示',
                        message: res.message,
                    });
                }
            })
            .catch(err => { console.log(err) });
    },
    getRateData(context,params){
        Vue.prototype.$newHttp.post('/userEvaluate/get',params)
            .then(res => {
                if (res.status === 0) {
                    MessageBox.alert(res.message);
                } else if (res.status === 1) {
                    context.commit('setRateData',res.data);
                }
            }
        ).catch(err => {console.log(err)});
    },
    insertRate(context,params){
        Vue.prototype.$newHttp.post('/userEvaluate/insert',params)
            .then(res => {
                if (res.status === 0) {
                    MessageBox.alert(res.message);
                } else if (res.status === 1) {
                    MessageBox.alert('评价成功').then( () => {
                        router.push('/orderList');
                    })
                }
            }
        ).catch(err => {console.log(err)});
    },
    makeVerifyCode(context,params){
        Vue.prototype.$newHttp.post('/smsVerifyCode/send',params)
            .then(res => {
                if (res.status === 0) {
                    MessageBox({
                        title: '提示',
                        message: res.message,
                    });
                }
            })
            .catch(err => {console.log(err)});
    },
    makeRecommend(context,params){
        Vue.prototype.$newHttp.post('/userRecommend/insert',params)
            .then(res => {
                if (res.status === 0) {
                    MessageBox.alert(res.message);
                } else if (res.status === 1) {
                    MessageBox.alert('推荐成功').then( () => {
                        router.push('/tuijianlist');
                    })
                }
            }
        ).catch(err => {console.log(err)});
    },
    checkRecommondList(context){
        Vue.prototype.$newHttp.post('/userRecommend/list',{})
            .then(res => {
                if (res.status === 0) {
                    MessageBox.alert(res.message);
                } else if (res.status === 1) {
                    // console.log(res)
                    context.commit('setRecommondList',res.data);
                }
            }
        ).catch(err => {console.log(err)});
    },
    checkSignature(context){
        Vue.prototype.$newHttp.post('/userSignature/get',{})
            .then(res => {
                if (res.status === 0) {
                    MessageBox.alert(res.message);
                } else if (res.status === 1) {
                    context.commit('setSignature',res.data);
                }
            }
        ).catch(err => {console.log(err)});
    },
    makeSignature(){
        Vue.prototype.$newHttp.post('/userSignature/insert',{})
            .then(res => {
                if (res.status === 0) {
                    MessageBox.alert(res.message);
                } else if (res.status === 1) {
                    MessageBox.alert('签到成功').then( () => {
                        router.go(0);
                    })
                }
            }
        ).catch(err => {console.log(err)});
    },
    getIntegralGoods(context){
        Vue.prototype.$newHttp.post('/integralGoods/list',{})
            .then(res => {
                if (res.status === 0) {
                    MessageBox.alert(res.message);
                } else if (res.status === 1) {
                    context.commit('setIntegralGoods',res.data);
                }
            }
        ).catch(err => {console.log(err)});
    },
    getGoodsDetail(context,params){
        Vue.prototype.$newHttp.post('/integralGoods/detail',params)
            .then(res => {
                if (res.status === 0) {
                    MessageBox.alert(res.message);
                } else if (res.status === 1) {
                    context.commit('setGoodsDetail',res.data);
                }
            }
        ).catch(err => {console.log(err)});
    },
    getUserInfoUpdate(context){
        Vue.prototype.$newHttp.post('/userAccount/getBindUserInfo',{})
            .then(res => {
                if (res.status === 0) {
                    MessageBox.alert(res.message);
                } else if (res.status === 1) {
                    context.commit('setUserInfoUpdate',res.data);
                }
            }
        ).catch(err => {console.log(err)});
    },
    makeUserInfoUpdate(context,params){
        Vue.prototype.$newHttp.post('/userAccount/updateUserInfo',params)
            .then(res => {
                if (res.status === 0) {
                    MessageBox.alert(res.message);
                } else if (res.status === 1) {
                    MessageBox.alert(res.message).then(() => {
                        router.push('/person');
                    });
                }
            }
        ).catch(err => {console.log(err)});
    }
}